package zhihui.mobile.manager.address;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.apache.log4j.Logger;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zhihui.mobile.bean.address.Location;
import zhihui.mobile.bean.address.login.Area;
import zhihui.mobile.bean.address.login.City;
import zhihui.mobile.bean.address.login.Province;
import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.AllManagerObvs;
import zhihui.mobile.manager.IManager;
import zhihui.mobile.net.HttpUtils;

/**
 * Created by 张公铭 on 2017/9/14 0014.
 */

public class NewAddressManager implements IManager
{
    private Logger logger = Logger.getLogger(AddressManager.class);
    private static NewAddressManager manager;
    private List<Province> mAddressList = new ArrayList<>();
    private Province lastProvince = null;
    public int proPosition = 0;
    private City lastCity = null;
    public int cityPosition = 0;
    private Area lastArea = null;
    public int areaPosition = 0;

    //记录键值的省会信息
    private Map<String, Province> mMap = new HashMap<>();
    //键值的市级信息
    private Map<String, List<Area>> mCityMap = new HashMap<>();

    private NewAddressManager()
    {
        AllManagerObvs.getInstance().addManager(this);
    }

    private synchronized static void createdIns()
    {
        if (manager == null)
        {
            manager = new NewAddressManager();
        }
    }

    public Map<String, Province> getmMap()
    {
        if (mMap == null || mMap.size() <= 0)
        {
            initProvinceMap();
        }
        return mMap;
    }

    public Map<String, List<Area>> getmCityMap()
    {
        if (mCityMap == null || mCityMap.size() <= 0)
        {
            initCityMap();
        }
        return mCityMap;
    }

    public static NewAddressManager getInstance()
    {
        if (manager == null)
        {
            createdIns();
        }
        return manager;
    }

    public List<Province> getmAddressList()
    {
        return mAddressList;
    }

    public void setmAddressList(List<Province> list)
    {
        if (mAddressList == null)
        {
            mAddressList = new ArrayList<>();
        }
        mAddressList.clear();
        mAddressList.addAll(list);
    }

    private void initProvinceMap()
    {
        if (mMap != null && mMap.size() > 0)
        {
            return;
        }
        if (mMap == null)
        {
            mMap = new HashMap<>();
        }
        mMap.clear();
        for (Province province : mAddressList)
        {
            mMap.put(province.getId(), province);
        }
    }

    private void initCityMap()
    {
        if (mCityMap != null && mCityMap.size() > 0)
        {
            return;
        }
        if (mCityMap == null)
        {
            mCityMap = new HashMap<>();
        }
        mCityMap.clear();
        for (Province province : mAddressList)
        {
            List<City> list = province.getCity();
            for (City city : list)
                mCityMap.put(city.getId(), city.getArea());
        }
    }

    public void getAddress(Location location, IHttpCallBackListener listener)
    {
        String url = "http://api.map.baidu.com/geosearch/v3/nearby";
        Map<String, Object> params = new HashMap<>();
        params.put("ak", "9Up47mjWP22L8mVWvksUDZUHxGlLLUGQ");
        params.put("geotable_id", "171445");
        params.put("location", location.getLongitude() + "," + location.getLatitude());
        params.put("radius", "3000");
        HttpUtils.get(url, params, listener);
    }

    public void getCommunityList(Area area, IHttpCallBackListener listener)
    {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("service", "house");
        paramsMap.put("method", "findhouseBycitycode");

        List<Object> values = new ArrayList<>();
        values.add(area.getId());

        HttpUtils.postAes(Config.URL, paramsMap, values, listener);
    }

    public Province getLastProvince()
    {
        return lastProvince;
    }

    public City getLastCity()
    {
        return lastCity;
    }

    public Area getLastArea()
    {
        return lastArea;
    }

    public void setLastProvince(Province lastProvince)
    {
        this.lastProvince = lastProvince;
    }

    public void setLastCity(City lastCity)
    {
        this.lastCity = lastCity;
    }

    public void setLastArea(Area lastArea)
    {
        this.lastArea = lastArea;
    }

    public boolean ininProvince(Context context)
    {
        boolean result = false;
        try
        {
            InputStream is = context.getAssets().open("area.json");
            byte[] bytes = new byte[is.available()];
            is.read(bytes);
            is.close();
            String json = new String(bytes);
            Gson gson = new Gson();
            List<Province> list = gson.fromJson(json, new TypeToken<List<Province>>()
            {
            }.getType());
           setmAddressList(list);
            result = true;
        } catch (Exception e)
        {
            result = false;
            e.printStackTrace();
            logger.error("解析地区文件错误：" + e.getMessage());
        }
        return result;
    }

    public void getCityInfoByUser(IHttpCallBackListener listener)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.wuliu.api.City");
        params.put("method", "findUse");

        HttpUtils.postAes(Config.URL, params, null, listener);
    }

    @Override
    public void clear()
    {
        AllManagerObvs.getInstance().removeManager(this);
        mAddressList.clear();
        mAddressList = null;
        mMap.clear();
        mMap = null;
        mCityMap.clear();
        mCityMap = null;
        lastProvince = null;
        lastCity = null;
        lastArea = null;
        proPosition = 0;
        cityPosition = 0;
        areaPosition = 0;
        manager = null;
    }
}
